package com.wuda.servlet.user;

import com.wuda.dao.BorrowRecordDao;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/user/returnBook")
public class ReturnBook extends HttpServlet {
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json; charset=utf8");

		int userId = Integer.parseInt(req.getParameter("userId"));
		int bookId = Integer.parseInt(req.getParameter("bookId"));

		BorrowRecordDao borrowRecordDao = new BorrowRecordDao();
		int result = borrowRecordDao.returnBook(userId, bookId);

		JSONObject jsonResult = new JSONObject();
		jsonResult.put("code", result);
		switch (result) {
			case 200:
				jsonResult.put("msg", "还书成功");
				break;
			case 201:
				jsonResult.put("msg", "用户没有借这本书");
				break;
			case 500:
				jsonResult.put("msg", "系统错误");
				break;
			default:
				break;
		}

		try (PrintWriter out = resp.getWriter()) {
			out.print(jsonResult);
			out.flush();
		}
	}
}
